import {
	REQ_CANCEL_ACTION,
	SET_REQ_CANCEL,
	SET_REQ_ENQUEUE,
	REQ_ENQUEUE_ACTION
} from '@/utils/Const';
import { Commit } from 'vuex';
import { Canceler } from 'axios';

interface State {
	value: Canceler[]
}

export default {
	namespaced: true,
	state: {
		value: []
	},
	mutations: {
		[SET_REQ_CANCEL](state: State) {
			state.value.forEach((reqCancelFn) => reqCancelFn());
			state.value = [];
		},
		[SET_REQ_ENQUEUE](state: State, payload) {
			state.value.push(payload);
		}
	},
	actions: {
		[REQ_CANCEL_ACTION](context: { commit: Commit }) {
			context.commit(SET_REQ_CANCEL);
		},
		[REQ_ENQUEUE_ACTION](context: { commit: Commit }, payload) {
			context.commit(SET_REQ_ENQUEUE, payload);
		}
	}
};
